package cn.wolfcode.luowowo.website.advice;

import cn.wolfcode.luowowo.common.exception.LogicalException;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import com.alibaba.fastjson.JSON;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 控制器增强
 * 1. 统一异常管理
 * 2. 时间
 */
@ControllerAdvice
public class CommonExceptionHandler {

    //异常处理标签, 参数: 要统一处理的异常类
    @ExceptionHandler(LogicalException.class)
    @ResponseBody
    public Object logicalExp(Exception e, HttpServletResponse resp) throws IOException {
        e.printStackTrace();
        return new AjaxResult(false, e.getMessage());
    }

    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public Object runTimeExp(Exception e, HttpServletResponse resp) throws IOException {
        e.printStackTrace();
        return new AjaxResult(false, "系统出现异常, 请联系管理员");
    }
}
